//
//  UIButton+SJEventInterval.m
//  eBondMall
//
//  Created by 赵世杰 on 2020/6/6.
//  Copyright © 2020 zhaoshijie. All rights reserved.
//

#import "UIButton+SJEventInterval.h"
#import "SwizzlingDefine.h"

static NSInteger eventInterval = 0.65;

@interface UIButton ()


//  是否能够点击
@property(nonatomic,assign)BOOL eventUnavailable;


@end


@implementation UIButton (SJEventInterval)


+ (void)load
{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
    
        SEL action = @selector(sendAction:to:forEvent:);
        SEL sj_action = @selector(sj_sendAction:to:forEvent:);
        
        
        swizzling_exchangeMethod(self, action, sj_action);
        
    });
    
}



- (void)sj_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event
{
    if([self isKindOfClass:[UIButton class]])
    {
        if(self.eventUnavailable == NO)
        {
            self.eventUnavailable = YES;
            
            [self sj_sendAction:action to:target forEvent:event];
            
            [self performSelector:@selector(setDafaulutVaule) withObject:nil afterDelay:eventInterval];
            
        }
    }
    else
    {
        [self sj_sendAction:action to:target forEvent:event];
    }
}


- (void)setDafaulutVaule
{
    self.eventUnavailable = NO;
}

- (void)setEventUnavailable:(BOOL)eventUnavailable
{
    objc_setAssociatedObject(self, @selector(eventUnavailable), @(eventUnavailable), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (BOOL)eventUnavailable
{
    return [objc_getAssociatedObject(self, _cmd) boolValue];
}




@end
